//
//  PackageCourseVC.m
//  ZhiBoYuan
//
//  Created by JW on 2018/4/23.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "PackageCourseVC.h"

#import "PackageListVC.h"
#import "MicroCourseModel.h"
#import "ABCell.h"
static NSString * identB = @"ABCell";

@interface PackageCourseVC ()<UITableViewDataSource,UITableViewDelegate>

//@property (nonatomic, strong) UITableView *tableView;
@property(nonatomic,strong)AxcEmptyDataTableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, assign)int page;
@property(nonatomic,strong)NSMutableDictionary *shaiXuanDic;
@end

@implementation PackageCourseVC

- (void)viewDidLoad {
    [super viewDidLoad];

    _page = 1;
    
    [self initWithViews];
    
    [self downloadPackageCourseDatas];
    [self getnote];

}

//接受通知页面方法

-(void)getnote
{
    //注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tongzhi:) name:@"AC" object:nil];
}

- (void)tongzhi:(NSNotification *)text
{
    
    NSLog(@" AC = %@",text.userInfo);
    self.shaiXuanDic= [NSMutableDictionary dictionary];
    
    self.shaiXuanDic = text.userInfo.mutableCopy;
    [self downloadPackageCourseDatas];

}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"AC"  object:nil];
}

- (void)downloadPackageCourseDatas{
        
    SGParameter *parameter  = [[SGParameter alloc] init];
    parameter.body = @{
                       @"page":[NSString stringWithFormat:@"%d",_page],
                       @"page_count":@"10",
                       @"course_type":@"3"// 1 直播 2微课 3套餐
                       };
    
    if ([self.shaiXuanDic allKeys].count > 0) {
        ShaiXuan *s = [[ShaiXuan alloc]initWithDictionary:self.shaiXuanDic];
        
        parameter.body = @{
                           @"xueduan_id":s.xueDuan,
                           @"xueke_id":s.xueKe,
                           @"nianji_id":s.nianJi,
                           @"knowledge_point_id":s.zhiShiDian,
                           @"is_free":s.leiXing,
                           //   @"list_type":@"",
                           
                           @"page":[NSString stringWithFormat:@"%d",_page],
                           @"page_count":@"10",
                           @"course_type":@"2"// 1 直播 2微课 3套餐
                           };
        
    }
    
    
    
    [SGHttpTool POST:Host_Append(CourseList) parameter:parameter sucess:^(id responseObject) {
        
        NSLog(@"%@",responseObject);
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (_page == 1) {
            [_dataArray removeAllObjects];
        }
   
//        ZYLog(@"%@",resultStr);
        SGReceive *receive = [SGReceive receiveWithResponseObject:responseObject dataWord:@"course_list" dataClass:[MicroCourseModel class]];

        for (MicroCourseModel *model in receive.data) {
            [_dataArray addObject:model];
        }
        
        if (_dataArray.count < 4) {
            _tableView.mj_footer.hidden = YES;
        }else{
            _tableView.mj_footer.hidden = NO;
        }
        
        //        [self.dataArray addObjectsFromArray:receive.data];
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
    
}


#pragma mark -- UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ABCell *cell = [tableView dequeueReusableCellWithIdentifier:identB forIndexPath:indexPath];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    cell.microModel = self.dataArray[indexPath.row];
    cell.Kebadage.hidden = YES;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PackageListVC *vc= [[PackageListVC alloc]init];
    vc.package_id = [self.dataArray[indexPath.row] microCourseID];
    vc.hidesBottomBarWhenPushed =YES;
    [self.navigationController pushViewController:vc animated:YES];
    
}

#pragma mark - 视图
- (void)initWithViews{
    [self.view addSubview:self.tableView];
}

#pragma makr -- property
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}




- (AxcEmptyDataTableView *)tableView {
    if (!_tableView) {
        AxcEmptyDataTableView *  tableView = [[AxcEmptyDataTableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, Screenheight- NAVIGATION_BAR_HEIGHT -TAB_BAR_HEIGHT) style:UITableViewStylePlain];
        tableView.backgroundColor = [UIColor clearColor];
        tableView.delegate        = self;
        tableView.dataSource      = self;
        tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        
        tableView.estimatedRowHeight = 44.0f;//推测高度，必须有，可以随便写多少
        
        tableView.rowHeight = UITableViewAutomaticDimension;//iOS8之后默认就是这个值，可以省略
        
        
        tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        
        //        self.extendedLayoutIncludesOpaqueBars = YES;
        
        if (@available(iOS 11.0, *)) {
            tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        if (iPhoneX) {
            tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
            tableView.scrollIndicatorInsets = _tableView.contentInset;
        }
        
        [tableView registerNib:[UINib nibWithNibName:identB bundle:nil] forCellReuseIdentifier:identB];
        
        [tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
        __weak __typeof(&*self)weakSelf = self;
        
        // 下拉刷新
        tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [self.shaiXuanDic removeAllObjects];
            
            _page = 1;
            
            [self downloadPackageCourseDatas];
            
            
        }];
        
        // 上拉加载
        tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            
            _page++;
            
            [self downloadPackageCourseDatas];
            
        }];
        
        
        self.placeholderView.frame = tableView.bounds;
        
        // 设置空集时候展示的View
        tableView.axcUI_placeHolderView = self.placeholderView;
        
        //  默认YES 自带渐入渐出动画效果，下边这个参数可以关闭
        tableView.axcUI_placeHolderViewAnimations = NO;
        
        // 占位View的“重新加载”按钮的Block回调:
        __weak typeof(self) WeakSelf = self;
        
        self.placeholderView.clickLoadBtnBlock = ^(id type) {
            //        WeakSelf.dataArray = nil;
            
            [WeakSelf.shaiXuanDic removeAllObjects];
            
            _page = 1;

            [WeakSelf downloadPackageCourseDatas];
            
        };
        
        
        

        _tableView = tableView;
        
    }
    return _tableView;
}

@end
